Page({
  data: {
    product: {},
    isFavorite: false, // 初始收藏状态
    user: {
      avatarUrl: '/images/touxiang1.png', 
      name: 'Erica',
      location: '广州'
    },
    imageList: [
      '/images/shangpin10.jpg', 
      '/images/shangpin2.jpg',
      '/images/shangpin3.jpg'
    ],
    inputValue: '' // 输入框的值
  },

  onLoad(options) {
    const productId = options.productId; // 从页面跳转时传递的商品ID
    console.log("Received product ID:", productId); // 调试信息
    this.loadProductDetails(productId);
    this.checkFavoriteStatus(productId);
  },

  // 加载商品详情
  loadProductDetails(productId) {
    console.log("Loading product details for ID:", productId); // 调试信息
    // 假数据
    const fakeProducts = {
      '001': {
        id: '001',
        imgSrc: '/images/message-touxiang1.png',
        title: '回收纯手工穿戴甲款式都要 高价回收 没有大瑕疵都',
        price: 6,
        demand: '74人想要',
        userNickname: 'Erica',
        location: '广州',
        shipping: '包邮',
        condition: '全新',
        tags: ['全新', 'M码', '包邮', '广州'],
        imageList: [
          '/images/shangpin1.jpg', 
          '/images/shangpin1.jpg',
          '/images/shangpin1.jpg'
        ],
        commentCount: 5,
        comments: [
          { text: '很好看，想要', nickname: '呱呱',avatar:'' },
          { text: '价格能便宜点吗？', nickname: '小白' }
        ]
      },
      '002': {
        id: '002',
        imgSrc: '/images/message-touxiang2.png',
        title: '50根黑色皮筋女扎头',
        price: 5,
        demand: '670人收藏',
        userNickname: 'Erica',
        location: '广州',
        shipping: '包邮',
        condition: '全新',
        tags: ['全新', 'M码', '包邮', '广州'],
        imageList: [
          '/images/shangpin2.jpg',
          '/images/shangpin2.jpg',
          '/images/shangpin2.jpg'
        ],
        commentCount: 5,
        comments: [
          { text: '很好看，想要', nickname: '呱呱' },
          { text: '价格能便宜点吗？', nickname: '小白' }
        ]
      },
      '003':{
        id: '003',
        imgSrc: '/images/message-touxiang3.png',
        title: '十三余家的遇见神鹿m码铃铛坏了 其他都',
        price: 30,
        demand: '211人想要',
        userNickname: 'Erica',
        location: '广州',
        
        shipping: '包邮',
        condition: '全新',
        tags: ['全新', 'M码', '包邮', '广州'],
        imageList: [
          '/images/shangpin3.jpg', 
          '/images/shangpin3.jpg',
          '/images/shangpin3.jpg'
        ],
        commentCount: 5,
        comments: [
          { text: '很好看，想要', nickname: '呱呱' },
          { text: '价格能便宜点吗？', nickname: '小白' }
        ]
      },
      '004':{
        id: '004',
        imgSrc: '/images/message-touxiang1.png',
        title: '【学生党】本人自用的',
        price: 6,
        demand: '74人想要',
        userNickname: 'Erica',
        location: '广州',
        shipping: '包邮',
        condition: '全新',
        tags: ['全新', 'M码', '包邮', '广州'],
        imageList: [
          '/images/shangpin4.jpg', 
          '/images/shangpin4.jpg',
          '/images/shangpin4.jpg'
        ],
        commentCount: 5,
        comments: [
          { text: '很好看，想要', nickname: '呱呱' },
          { text: '价格能便宜点吗？', nickname: '小白' }
        ]
      },
      '005':{
        id: '005',
        imgSrc: '/images/message-touxiang2.png',
        title: '洗衣机全新未拆宿舍适用特价',
        price: 240,
        demand: '670人收藏',
        userNickname: 'Erica',
        location: '广州',
        shipping: '包邮',
        condition: '全新',
        tags: ['全新', 'M码', '包邮', '广州'],
        imageList: [
          '/images/shangpin5.jpg', 
          '/images/shangpin5.jpg',
          '/images/shangpin5.jpg'
        ],
        commentCount: 5,
        comments: [
          { text: '很好看，想要', nickname: '呱呱' },
          { text: '价格能便宜点吗？', nickname: '小白' }
        ]
      },
      '006':{
        id: '006',
        imgSrc: '/images/message-touxiang3.png',
        title: '艾米兔正版包包 在tb压的货TB压货现清',
        price: 10,
        demand: '534人想要',
        userNickname: 'Erica',
        location: '广州',
        shipping: '包邮',
        condition: '全新',
        tags: ['全新', 'M码', '包邮', '广州'],
        imageList: [
          '/images/shangpin6.jpg', 
          '/images/shangpin6.jpg',
          '/images/shangpin6.jpg'
        ],
        commentCount: 5,
        comments: [
          { text: '很好看，想要', nickname: '呱呱' },
          { text: '价格能便宜点吗？', nickname: '小白' }
        ]
      }
    };

    const product = fakeProducts[productId];
    if (product) {
      this.setData({
        product: product
      });
    } else {
      console.error("Product not found in fake data with ID:", productId); // 调试信息
      wx.showToast({
        title: '商品详情加载失败',
        icon: 'none'
      });
    }
  },

  // 检查商品是否已被收藏
  checkFavoriteStatus(productId) {
    const userFavorites = wx.getStorageSync('userFavorites') || [];
    this.setData({
      isFavorite: userFavorites.indexOf(productId) > -1
    });
  },

  // 处理收藏按钮点击事件
  toggleFavorite() {
    const productId = this.data.product.id;
    if (this.data.isFavorite) {
      this.cancelFavorites(productId);
    } else {
      this.addFavorites(productId);
    }
  },

  // 添加到收藏
  addFavorites(productId) {
    const userFavorites = wx.getStorageSync('userFavorites') || [];
    if (userFavorites.indexOf(productId) === -1) {
      userFavorites.push(productId);
      wx.setStorageSync('userFavorites', userFavorites);
      this.setData({
        isFavorite: true
      });
      wx.showToast({
        title: '收藏成功',
        icon: 'success'
      });
    } else {
      wx.showToast({
        title: '已经收藏过',
        icon: 'none'
      });
    }
  },

  // 从收藏中移除
  cancelFavorites(productId) {
    const userFavorites = wx.getStorageSync('userFavorites') || [];
    const index = userFavorites.indexOf(productId);
    if (index > -1) {
      userFavorites.splice(index, 1);
      wx.setStorageSync('userFavorites', userFavorites);
      this.setData({
        isFavorite: false
      });
      wx.showToast({
        title: '取消收藏成功',
        icon: 'success'
      });
    } else {
      wx.showToast({
        title: '未收藏此商品',
        icon: 'none'
      });
    }
  },

  // 处理输入框变化
  onInputChange(e) {
    this.setData({
      inputValue: e.detail.value
    });
  },

  // 提交留言
  submitCommentOnEnter(e) {
    if (e.detail.value.trim() === '') {
      wx.showToast({
        title: '留言不能为空',
        icon: 'none'
      });
      return;
    }
    // 这里可以添加提交留言的逻辑
    wx.showToast({
      title: '留言成功',
      icon: 'success'
    });
    this.setData({
      inputValue: '' // 清空输入框
    });
  },

  // 导航到其他页面
  navigateToPage() {
    wx.navigateTo({
      url: '/pages/maijia/maijia' // 替换为实际的页面路径
    });
  }
});